package manager.helpers.table;

import java.util.LinkedList;
import javax.swing.table.AbstractTableModel;
import manager.interfaces.InterfaceTable;

public class TableModel extends AbstractTableModel {

    private LinkedList data;
    private LinkedList<Integer> idList;
    private LinkedList<String> columns;
    private int rowCount;
    private TableRender tableRender;
    private InterfaceTable interfaceTable;

    public TableModel(LinkedList<String> newColumns, TableRender newTableRender, InterfaceTable newInterfaceTable) {
        data = new LinkedList();
        idList = new LinkedList<Integer>();
        rowCount = 0;
        columns = newColumns;
        tableRender = newTableRender;
        interfaceTable = newInterfaceTable;
    }

    public int getRowCount() {
        return rowCount;
    }

    public int getColumnCount() {
        return columns.size();
    }

    @Override
    public String getColumnName(int column) {
        return columns.get(column);
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return ((LinkedList) data.get(columnIndex)).get(rowIndex);
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if ((Boolean) aValue) {
            int columnSize = columns.size();
            for (int i = 0; i < columnSize; i++) {
                if (getColumnClass(i) == Boolean.class) {
                    for (int j = 0; j < rowCount; j++) {
                        ((LinkedList) data.get(i)).set(j, false);
                    }
                }
            }
            tableRender.setSelectedRow(rowIndex);
            if (columnIndex == 0) {
                interfaceTable.tableAction(idList.get(rowIndex), -1, rowIndex);
            } else {
                interfaceTable.tableAction(idList.get(rowIndex), 1, rowIndex);
            }
        } else {
            tableRender.setSelectedRow(-1);
        }
        ((LinkedList) data.get(columnIndex)).set(rowIndex, aValue);
        this.fireTableDataChanged();
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        return rowCount > 0 ? ((LinkedList) data.get(columnIndex)).get(0).getClass() : String.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return interfaceTable.isEditableColumn(columnIndex);
    }

    public void update(LinkedList new_data) {
        if (new_data.size() > 0) {
            data = new LinkedList();
            rowCount = ((LinkedList) new_data.get(0)).size();
            idList = (LinkedList<Integer>) new_data.get(0);
            for (int i = 1; i < new_data.size(); i++) {
                data.add(new_data.get(i));
            }
        } else {
            data = new LinkedList();
            rowCount = 0;
        }
        tableRender.setSelectedRow(-1);
        this.fireTableDataChanged();
    }

    public LinkedList getData() {
        return data;
    }

    public LinkedList<Integer> getIdList() {
        return idList;
    }
}
